package org.qqcui.designpattern.patterns.proxy.dynamic;

import java.lang.annotation.Target;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * 动态代理类，使用JDK动态代理实现方式来实现
 *
 * @author cuiqingqiang
 * @version 1.0
 * @date 2022/5/1 10:19
 **/
public class ProxyFactory {

    private Object target;

    public ProxyFactory(Object target) {
        this.target = target;
    }

    public Object getProxyInstance() {
        return Proxy.newProxyInstance(target.getClass().getClassLoader(),
                target.getClass().getInterfaces(), new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                System.out.println("JDK代理开始");
                Object returnVal = method.invoke(target, args);
                System.out.println("JDK代理提交");
                return returnVal;
            }
        });
    }
}
